## This code include scripts for Appendix Figures
# Figure A2


rm(list = ls())
library(tidyverse)
library(ggrepel)
library(ggtext)
library(dplyr)
library(showtext)
library(tidyr)
library(showtext)
library(ggplot2)
library(randomForest)
library(dplyr)
library(forcats)
library(randomForest)


load("Final_JPR/data/paperdata.RData")
data <- paperdata %>%
       filter(tek_count>0)



set.seed(1014)

RF_base <- randomForest(incidence_flag ~ best_tlineq_total + worst_tlineq_total + median_tlineq_total + 
                               nearst_tlineq_total + lineq_total + status_excl + lsize + 
                               family_warhistdummy + family_downgraded2 + ctygdppc_log + 
                              ctypop_log, 
                        data =data, importance=TRUE, na.action=na.omit)


data_rf <- as.data.frame(cbind(rownames(RF_base$importance),round(RF_base$importance[,"IncNodePurity"],1)))
colnames(data_rf) <- c("Parameters","IncNodePurity")
data_rf$IncNodePurity <- as.numeric(as.character(data_rf$IncNodePurity))
# re-ordering
data_rf<- data_rf %>% 
       dplyr::mutate(name = fct_reorder(Parameters, IncNodePurity)) 

ggplot(data_rf, aes(IncNodePurity,name)) + geom_point() +
       labs(x = "Mean Decrease in Node Impurity", y = "",
           title = "Variable Importance in Random Forest Model",
           caption = "Conflict Incidence")+
            scale_y_discrete(labels = rev(c("Previous rebellions",
                                        "Ln(Country population)",
                                        "Relative group size",
                                        "Ln(Country GDP per capita)",
                                        "Transnational inequality (best)",
                                        "Transnational inequality (nearest)",
                                        "Transnational inequality (median)",
                                        "Horizontal inequality",
                                        "Transnational inequality (worst)",
                                        "Status excluded",  
                                        "Status downgraded"))) + 
       theme_bw() + 
       theme(axis.text= element_text(size=15),
             text = element_text(size=15))
#ggsave("Final_JPR/Figures/RF_incidence.pdf", width =11, height = 7)
ggsave("Final_JPR/Figures/appendix_figures/fig_A2.pdf", width =11, height = 7, units='in', dpi=600)

